﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace HCMServices.DAO
{
    [Table(Name = "TUKHOAQUANHUYEN")]
    public class KeyDistrictDAO
    {
        [Column(Name = "MaTuKhoaQuanHuyen", IsDbGenerated = true, IsPrimaryKey = true, DbType = "int")]
        public int MaTuKhoaQuanHuyen { get; set; }

        [Column(Name = "TuKhoaQuanHuyen", DbType = "nvarchar(64)")]
        public string TuKhoaQuanHuyen { get; set; }

        [Column(Name = "MaQuanHuyen", DbType = "int")]
        public int MaQuanHuyen { get; set; }

        private MyDataContext db;

        public KeyDistrictDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        public List<KeyDistrictDTO> Search(string strInput)
        {
            List<KeyDistrictDTO> lstTuKhoaQuanHuyenDTO = new List<KeyDistrictDTO>();

            IList<TUKHOAQUANHUYEN> query = (from TUKHOAQUANHUYEN tkqh in db.KeyDistrict
                                            where tkqh.TuKhoaQuanHuyen1 == strInput
                                            select tkqh).ToList();

            foreach (TUKHOAQUANHUYEN tkqh in query)
            {
                if (tkqh.MaQuanHuyen != null)
                {
                    KeyDistrictDTO temp = new KeyDistrictDTO();
                    temp.DistrictId = (int)tkqh.MaQuanHuyen;                    // tkqh.MaQuanHuyen maybe null
                    temp.KeyDistrict = tkqh.TuKhoaQuanHuyen1;
                    temp.KeyDistrictId = tkqh.MaTuKhoaQuanHuyen;

                    lstTuKhoaQuanHuyenDTO.Add(temp);
                }
            }

            return lstTuKhoaQuanHuyenDTO;
        }

        public Dictionary<string, string> GetDictionaryOfKey()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            var query = (from tkqh in db.KeyDistrict
                         from qh in db.District
                         where tkqh.MaQuanHuyen == qh.MaQuanHuyen
                         select new { tkqh.TuKhoaQuanHuyen1, qh.TenQuanHuyen }).ToList();

            foreach (var t in query)
            {
                if (t.TuKhoaQuanHuyen1 != null && !result.ContainsKey(t.TuKhoaQuanHuyen1))
                    result.Add(t.TuKhoaQuanHuyen1, t.TenQuanHuyen);
            }

            return result;
        }
    }
}
